package com.heima.a07_demo.server.controller;

import com.heima.a07_demo.common.result.Result;
import com.heima.a07_demo.pojo.dto.DoctorDTO;
import com.heima.a07_demo.pojo.dto.DoctorImageDTO;
import com.heima.a07_demo.pojo.dto.DoctorPasswordDTO;
import com.heima.a07_demo.pojo.dto.DoctorRegisterDTO;
import com.heima.a07_demo.pojo.entity.Doctor;
import com.heima.a07_demo.pojo.vo.DoctorVO;
import com.heima.a07_demo.server.service.DoctorService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/doctor")
@Slf4j
public class DoctorController {

    @Autowired
    private DoctorService doctorService;;
    /**
     * 退出登陆
     *
     * @return
     */
    //TODO 如果要求不是很严格就这样，让前端去跳转就行，如果严格点就把jwt令牌回收掉，到时候可前后端练调的时候再弄
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /*
    * 新增医生/注册
    * */
    @PostMapping("/register")
    public Result save(@RequestBody DoctorRegisterDTO doctorRegisterDTO){
        log.info("新增医生：{}",doctorRegisterDTO);
        doctorService.save(doctorRegisterDTO);
        return Result.success();
    }
    /*
    * 根据id查询医生信息
    * */
    @GetMapping("/{id}")
    public Result<DoctorVO> getById(@PathVariable Long id){
        Doctor doctor=doctorService.getById(id);
        DoctorVO doctorVO=new DoctorVO();
        BeanUtils.copyProperties(doctor,doctorVO);
        //TODO 搞不懂为什么返回的doctorId会为null
        log.info("医生信息:{}",doctor);
        doctorVO.setDoctorId(id);
        return Result.success(doctorVO);
    }
/*
* 修改医生信息
* */
    @PutMapping("/update")
    public Result update(@RequestBody DoctorDTO doctorDTO){
        log.info("doctorId为:{}",doctorDTO.getDoctorId());
        log.info("编辑员工信息{}",doctorDTO );
       doctorService.update(doctorDTO);
        return Result.success();
    }
    /*
    *
    * 根据id查询医生头像
    * */
    @GetMapping("/image/{id}")
    public Result<String> getImage(@PathVariable Long id){
        log.info("医生id为：{}",id);
        String filepath=doctorService.getImageById(id);
        return Result.success(filepath);
    }
    /*
    * 修改头像
    * */
    @PutMapping("/update/image")
    public Result updateImage(@RequestBody DoctorImageDTO doctorImageDTO){
        log.info("id为：{},新的文件路径是:{}",doctorImageDTO.getDoctorId(),doctorImageDTO.getImage());
        doctorService.updateImage(doctorImageDTO);
        return Result.success();
    }
    /*
    * 修改密码
    * */
    @PutMapping("/update/password")
    public Result updatePassword(@RequestBody DoctorPasswordDTO doctorPasswordDTO){
        log.info("旧密码:{}和新密码为:{}",doctorPasswordDTO.getOldPassword(),doctorPasswordDTO.getNewPassword());
        doctorService.updatePassword(doctorPasswordDTO);
        return Result.success();
    }

}
